package cn.xluobo.business.wechat.miniapp.report.service;

import cn.xluobo.business.sc.course.domain.req.cla.ReqClaCount;
import cn.xluobo.business.sc.course.domain.req.time.ReqClaTimeCount;
import cn.xluobo.business.sc.course.repo.mapper.ScCourseClaMapper;
import cn.xluobo.business.sc.course.repo.mapper.ScCourseMapper;
import cn.xluobo.business.sc.course.service.IScClaTimeAttendService;
import cn.xluobo.business.sc.course.service.IScClaTimeService;
import cn.xluobo.business.sc.student.repo.mapper.ScStudentMapper;
import cn.xluobo.business.sys.admin.enums.RoleEnum;
import cn.xluobo.business.sys.admin.service.ISysUserService;
import cn.xluobo.business.wechat.miniapp.report.domain.resp.RespMineTopValue;
import cn.xluobo.config.login.LoginUser;
import cn.xluobo.core.page.ReqDeptCondition;
import cn.xluobo.utils.LoginUserUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @author ：KingLong
 * @date ：Created in 2020/10/22 21:31
 */
@Service
public class WechatMiniReportService {

    @Autowired
    private IScClaTimeAttendService claTimeAttendService;
    @Autowired
    private IScClaTimeService claTimeService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ScStudentMapper studentMapper;
    @Autowired
    private ScCourseClaMapper claMapper;
    @Autowired
    private ScCourseMapper courseMapper;

    /**
     * 我的页面 顶部指标
     * @return
     */
    public RespMineTopValue mineTopValue() {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        Long deptId = loginUser.getDeptId();

        // 是否拥有管理角色
        boolean allSchoolData = userService.checkUserHasAnyRole(loginUser.getUserId(), loginUser.getNowTenantId(), new String[]{
                RoleEnum.SCHOOL_MANAGER.getRoleCode(),
                RoleEnum.TEACHING_MANAGER.getRoleCode()
        });

        String thisMonthBegin = DateTime.now().withDayOfMonth(1).toString("yyyy-MM-dd");
        String thisMonthEnd = DateTime.now().plusMonths(1).withDayOfMonth(1).minusDays(1).toString("yyyy-MM-dd");
        String today = DateTime.now().toString("yyyy-MM-dd");

        if(allSchoolData) {

            ReqDeptCondition reqDeptCondition = new ReqDeptCondition();
            reqDeptCondition.setDeptId(deptId);
            reqDeptCondition.setUserId(loginUser.getUserId());
            Integer studentCnt = studentMapper.selectStudentCount(reqDeptCondition);
            Integer courseCnt = courseMapper.selectCourseCount(reqDeptCondition);

            ReqClaCount reqClaCount = ReqClaCount.builder().build();
            reqClaCount.setDeptId(deptId);
            reqClaCount.setUserId(loginUser.getUserId());
            Integer claCnt = claMapper.selectClaCount(reqClaCount);

            return RespMineTopValue.builder()
                    .studentCnt(studentCnt)
                    .claCnt(claCnt)
                    .courseCnt(courseCnt)
                    .build();
        } else {
            ReqClaTimeCount reqTeacherGetHour = ReqClaTimeCount.builder().beginDate(thisMonthBegin).endDate(thisMonthEnd).teacherId(loginUser.getStaffId()).build();
            reqTeacherGetHour.setDeptId(deptId);
            reqTeacherGetHour.setUserId(loginUser.getUserId());
            BigDecimal thisMonthGetHour = claTimeAttendService.getTeacherGetHour(reqTeacherGetHour);

            ReqClaCount reqClaCount = ReqClaCount.builder().teacherId(loginUser.getStaffId()).build();
            reqClaCount.setDeptId(deptId);
            reqClaCount.setUserId(loginUser.getUserId());
            Integer teacherClaCnt = claMapper.selectClaCount(reqClaCount);

            ReqClaTimeCount reqClaTimeCount = ReqClaTimeCount.builder().beginDate(today).endDate(today).hadBegin(false).teacherId(loginUser.getStaffId()).build();
            reqClaTimeCount.setDeptId(deptId);
            reqClaTimeCount.setUserId(loginUser.getUserId());
            reqClaTimeCount.setHadBegin(false);
            Integer todayWaitClaTime = claTimeService.claTimeCount(reqClaTimeCount);

            return RespMineTopValue.builder()
                    .thisMonthGetHour(thisMonthGetHour)
                    .teacherClaCnt(teacherClaCnt)
                    .todayWaitClaTime(todayWaitClaTime)
                    .build();
        }

    }

}
